package com.tianbing.adlottery.controller.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.entity.TagLog;
import com.fengniu.ejb.entity.TagLogLink;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditTagLogController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditTagLogController.class.getName());

	private IUserBusiness userBusiness;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		//		
		ControllerFormBean fb = new ControllerFormBean();
		fb.setTagLog(new TagLog());
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ModelAndView mv = new ModelAndView(this.getSuccessView());
		Map model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean form = (ControllerFormBean) cmd;
		TagLog tagLog = form.getTagLog();
		if (tagLog != null) {

			int[] targetUrlIdxs = ServletRequestUtils.getIntParameters(request, "targetUrlIdx");
			String[] targetUrls = ServletRequestUtils.getStringParameters(request, "targetUrl");
			String[] targetUrlNames = ServletRequestUtils.getStringParameters(request, "targetUrlName");
			List<TagLogLink> tagLogLinks = new ArrayList<TagLogLink>(0);
			if (targetUrlIdxs != null && targetUrlIdxs.length > 0) {
				String tUrls = "";
				for (int i = 0; i < targetUrlIdxs.length; i++) {
					int j = targetUrlIdxs[i];
					if (targetUrls.length > j && targetUrlNames.length > j) {
						tUrls += " " + targetUrls[j].trim() + "<!--【" + targetUrlNames[j] + "】-->";
						TagLogLink tagLogLink = new TagLogLink();
						tagLogLink.setTagLog(tagLog);
						tagLogLink.setTitle(targetUrlNames[j]);
						tagLogLink.setUrl(targetUrls[j].trim());
						tagLogLinks.add(tagLogLink);
					}
				}
				tagLog.setTagLogLinks(tagLogLinks);
				logger.info("targetUrls = " + tUrls);
			}

			tagLog = userBusiness.saveTagLog(auth.getUserSysId(), tagLog);
		}
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		return model;
	}

	/**
	 * @param userBusiness the userBusiness to set
	 */
	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	/**
	 * @param skinUtils the skinUtils to set
	 */
	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

}
